package com.ymatou.iapi.optrade.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.ChangeEcommerceFlagReq;
import com.ymatou.iapi.optrade.parameter.req.DeliveryTimeoutReq;
import com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.optrade.parameter.vo.OrderGroupVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderItemVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderVo;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.ChangeEcommerceFlagCall;
import com.ymatou.iapi.optrade.service.DeliveryTimeoutCall;
import com.ymatou.iapi.optrade.service.IdCardService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingProductVo;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
 * 电商件已接单48小时，消息提醒 FreeMaker Template自动生成代码
 */
@TestSuite
@Ignore
public class Ts_DeliveryTimeout {
	
	private static DeliveryTimeoutReq deliveryTimeoutReq;
	private static DeliveryTimeoutCall deliveryTimeoutCall;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
	private static String sellerName = String.valueOf(EnvSetup.getData("autotest_sellerName1"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("电商件已接单48小时,消息提醒");
	}

	@Before
	public void caseUp() {
		deliveryTimeoutReq = new DeliveryTimeoutReq();
		deliveryTimeoutCall = new DeliveryTimeoutCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(deliveryTimeoutCall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private static void changeEcommerceFlagCall(long orderId, int ecommerceFlag)
			throws ClientProtocolException, IOException {
		ChangeEcommerceFlagReq changeEcommerceFlagReq = new ChangeEcommerceFlagReq();
		ChangeEcommerceFlagCall changeEcommerceFlagCall = new ChangeEcommerceFlagCall();

		changeEcommerceFlagReq.setOrderId(orderId);
		changeEcommerceFlagReq.setEcommerceFlag(ecommerceFlag);
		changeEcommerceFlagReq.setAppId("autotest");
		changeEcommerceFlagReq.setBillCode("autotest111");

		changeEcommerceFlagCall.setData(changeEcommerceFlagReq);
		changeEcommerceFlagCall.callService();
	}

	/**
	 * 订单为电商件,物流为贝海直邮
	 * 
	 * @return PlaceOrderResp
	 */
	public TradingServiceResp placeOrder(PlaceOrderReq placeOrderReq) {

		OrderVo orderVo = placeOrderReq.getOrderGroups().get(0).getOrders()
				.get(0);

		orderVo.setEcommerceFlag(true);

		orderVo.setLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);
		orderVo.setOfficialLogistics(true);

		return TradingCallService.callPlaceOrderService(placeOrderReq);

	}

	@Test
	@Category(P1.class)
	@TestCase
	// order_accepted_no_dispatch_notify 消息
	public void Tc_DeliveryTimeout_001() {
		Logger.start(true, "电商件,物流为贝海直邮,用户没上传过身份证号,推送消息提醒");
		try {

			PlaceOrderReq placeOrderReq = TradingCallService
					.buildStandPlaceOrderReq();
			
			OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
			OrderVo orderVo = TradingCallService.createOrderVo();
			
			// 创建一个普通商品
			TradingProductVo product = TradingProductVo.createPro(sellerId,
					sellerName, 100);
			
			OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
					product.getProductId(), product.getCatalogId());
			
			orderVo.setOrderItems(Arrays.asList(orderItemVo));
			orderGroupVo.setOrders(Arrays.asList(orderVo));
			placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));
			
			placeOrderReq.setRecvName(TestDataManager.getChineseName());

			TradingServiceResp placeOrderResp = placeOrder(placeOrderReq);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);

			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			deliveryTimeoutReq.setOrderId(orderId);

			deliveryTimeoutCall.setData(deliveryTimeoutReq);

			deliveryTimeoutCall.callService();

			Logger.verifyEquals("ok", deliveryTimeoutCall.getReturnData(),
					"验证Code");

			// 数据库验证
			OrderPo actualOrder1 = orderShardingWapper
					.selectOrderbyOrderId(orderId);

			Logger.comment("校验用户是否已经上传身份证");

			Logger.verifyEquals(false, actualOrder1.getUploadedIdcard(),
					"验证数据库UploadedIdcard");

			Logger.verifyEquals(false, actualOrder1.getPredictEcommerce(),
					"验证数据库predictEcommerce");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DeliveryTimeout_002() {
		Logger.start(true, "电商件,物流为贝海直邮,用户上传过身份证号,不推送消息提醒");

		try {

			PlaceOrderReq placeOrderReq = TradingCallService
					.buildStandPlaceOrderReq();
			
			OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
			OrderVo orderVo = TradingCallService.createOrderVo();
			
			// 创建一个普通商品
			TradingProductVo product = TradingProductVo.createPro(sellerId,
					sellerName, 100);
			
			OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
					product.getProductId(), product.getCatalogId());
			
			orderVo.setOrderItems(Arrays.asList(orderItemVo));
			orderGroupVo.setOrders(Arrays.asList(orderVo));
			placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));

			long userId = placeOrderReq.getUserId();
			String key = "1234567890123456";

			YmtIdCardInfo ymtIdCardInfo = IdCardService.getIdCardByUserId(
					userId, 1, true, 0);

			placeOrderReq.setRecvName(ymtIdCardInfo.getName());
			placeOrderReq.setRecvMobile(CipherUtil.decryptAES(key,
					ymtIdCardInfo.getPhone()));

			TradingServiceResp placeOrderResp = placeOrder(placeOrderReq);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);

			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			deliveryTimeoutReq.setOrderId(orderId);

			deliveryTimeoutCall.setData(deliveryTimeoutReq);

			deliveryTimeoutCall.callService();

			Logger.verifyEquals("ok", deliveryTimeoutCall.getReturnData(),
					"验证Code");

			// 数据库验证
			OrderPo actualOrder1 = orderShardingWapper
					.selectOrderbyOrderId(orderId);

			Logger.comment("校验用户是否已经上传身份证");

			Logger.verifyEquals(true, actualOrder1.getUploadedIdcard(),
					"验证数据库UploadedIdcard");

			Logger.verifyEquals(false, actualOrder1.getPredictEcommerce(),
					"验证数据库predictEcommerce");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DeliveryTimeout_003() {
		Logger.start(true, "电商件,物流为贝海直邮,用户没上传过身份证号,接到过贝海通知实际为个人件,不推送消息提醒");
		try {

			PlaceOrderReq placeOrderReq = TradingCallService
					.buildStandPlaceOrderReq();
			
			OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
			OrderVo orderVo = TradingCallService.createOrderVo();
			
			// 创建一个普通商品
			TradingProductVo product = TradingProductVo.createPro(sellerId,
					sellerName, 100);
			
			OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
					product.getProductId(), product.getCatalogId());
			
			orderVo.setOrderItems(Arrays.asList(orderItemVo));
			orderGroupVo.setOrders(Arrays.asList(orderVo));
			placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));

			long userId = placeOrderReq.getUserId();

			placeOrderReq.setRecvName(TestDataManager.getChineseName());

			TradingServiceResp placeOrderResp = placeOrder(placeOrderReq);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);

			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			changeEcommerceFlagCall(orderId, 0);

			deliveryTimeoutReq.setOrderId(orderId);

			deliveryTimeoutCall.setData(deliveryTimeoutReq);

			deliveryTimeoutCall.callService();

			Logger.verifyEquals("ok", deliveryTimeoutCall.getReturnData(),
					"验证Code");

			// 数据库验证
			OrderPo actualOrder1 = orderShardingWapper
					.selectOrderbyOrderId(orderId);

			Logger.comment("校验用户是否已经上传身份证");

			Logger.verifyEquals(false, actualOrder1.getUploadedIdcard(),
					"验证数据库UploadedIdcard");

			Logger.verifyEquals(true, actualOrder1.getPredictEcommerce(),
					"验证数据库predictEcommerce");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_DeliveryTimeout_004() {
		Logger.start(true, "test");

		try {

			deliveryTimeoutReq.setOrderId(127133988);

			deliveryTimeoutCall.setData(deliveryTimeoutReq);

			deliveryTimeoutCall.callService();

			Logger.verifyEquals("ok", deliveryTimeoutCall.getReturnData(),
					"验证Code");

			Thread.sleep(3000);

			// 数据库验证
			OrderPo actualOrder1 = orderShardingWapper
					.selectOrderbyOrderId(deliveryTimeoutReq.getOrderId());

			Logger.comment("校验用户是否已经上传身份证");

			Logger.verifyEquals(false, actualOrder1.getUploadedIdcard(),
					"验证数据库UploadedIdcard");

			Logger.verifyEquals(true, actualOrder1.getPredictEcommerce(),
					"验证数据库predictEcommerce");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}